package com.wise.wing.activity.tools;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;

public class SMSBroadcastReceiver extends BroadcastReceiver {

	private MessageListener mMessageListener;
	public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
	private String vCode;

	public SMSBroadcastReceiver(String code, MessageListener listener) {
		super();
		vCode = code;
		mMessageListener = listener;
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent.getAction().equals(SMS_RECEIVED_ACTION)) {
			Object[] pdus = (Object[]) intent.getExtras().get("pdus");
			for (Object pdu : pdus) {
				SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
				// 短信内容
				String content = smsMessage.getDisplayMessageBody();
				System.out.println(content);
				// 过滤不需要读取的短信的发送号码
				if (content.indexOf(vCode) >= 0) {
					if (mMessageListener != null)
						mMessageListener.onReceived(vCode);
					abortBroadcast();
				}
			}
		}
	}

	public interface MessageListener {
		public void onReceived(String message);
	}

	public void setMessageListener(MessageListener mMessageListener) {
		this.mMessageListener = mMessageListener;
	}

	public String getCode() {
		return vCode;
	}

	public void setCode(String vCode) {
		this.vCode = vCode;
	}

}
